/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.FenetrePrincipaleControleur;
import Controleur.TableModel.IncidentTableModel;
import Controleur.TableModel.TechnicienTableModel;
import Controleur.TableModel.TypeIncTableModel;
import Controleur.TableModel.VeloTableMobel;
import Controleur.TypeFormulaire;
import Modele.ModelContainer;
import Modele.Technicien;
import Modele.TypeIncident;
import Modele.Velo;
import Utils.Constantes;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import nemovelo.NemoVelo;

/**
 *
 * @author 20cent
 */
public class FenetrePrincipale extends javax.swing.JFrame {

    private FenetrePrincipaleControleur controleur;

    /**
     * Creates new form FenetrePrincipale
     */
    public FenetrePrincipale() {
        initComponents();
        controleur = new FenetrePrincipaleControleur(this);
        controleur.recuperIncidents(null, null, null);
        controleur.recuperVelos();
        controleur.recuperTypesIncidents();
        controleur.recuperTechniciens();
        //on place les listener de double click pour les jTable
        setTableListeners();

        //on génère les comboBox
        cb_filter_technicien.removeAllItems();
        cb_filter_statut.removeAllItems();
        cb_filter_statut.addItem(null);
        cb_filter_statut.addItem(Constantes.STATUT_OUVERT);
        cb_filter_statut.addItem(Constantes.STATUT_EN_COURS);
        cb_filter_statut.addItem(Constantes.STATUT_TRAITE);
        cb_filter_statut.addItem(Constantes.STATUT_CLOS);
        cb_filter_priorite.removeAllItems();
        cb_filter_priorite.addItem(null);
        cb_filter_priorite.addItem(Constantes.PRIORITE_BASSE);
        cb_filter_priorite.addItem(Constantes.PRIORITE_MOYENNE);
        cb_filter_priorite.addItem(Constantes.PRIORITE_HAUTE);
        
        cb_filter_priorite.setSelectedIndex(0);
        cb_filter_statut.setSelectedIndex(0);
    }

    /**
     * appliquer le model au jTable
     *
     * @param model
     */
    public void appliquerVeloModel(VeloTableMobel model) {
        table_velo.setModel(model);
    }

    /**
     * appliquer le model au jTable
     *
     * @param model
     */
    public void appliquerIncidentModel(IncidentTableModel model) {
        table_incidents.setModel(model);
    }

    /**
     * appliquer le model au jTable
     *
     * @param model
     */
    public void appliquerTypeIncModel(TypeIncTableModel model) {
        table_typeinc.setModel(model);
    }

    /**
     * appliquer le model au jTable
     *
     * @param model
     */
    public void appliquerTechnicienModel(TechnicienTableModel model) {
        table_techniciens.setModel(model);
    }

    private void setTableListeners() {
        //listener pour le tableau type d'incidents
        table_typeinc.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                //intercepter le double click
                if (e.getClickCount() == 2) {
                    Point p = e.getPoint();

                    int row = table_typeinc.rowAtPoint(p);
                    if (row >= 0) {
                        controleur.afficherTypeIncDetail(row);
                    }
                }
            }
        });
        //listener pour le tableau techniciens
        table_techniciens.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    Point p = e.getPoint();

                    int row = table_techniciens.rowAtPoint(p);
                    if (row >= 0) {
                        controleur.afficherTechnicienDetail(row);
                    }
                }
            }
        });
        //listener pour le tableau vélo
        table_velo.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    Point p = e.getPoint();

                    int row = table_velo.rowAtPoint(p);
                    if (row >= 0) {
                        controleur.afficherVeloDetail(row);
                    }
                }
            }
        });
        //listener pour le tableau incident
        table_incidents.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    Point p = e.getPoint();

                    int row = table_incidents.rowAtPoint(p);
                    if (row >= 0) {
                        controleur.afficherIncidentDetail(row);
                    }
                }
            }
        });
    }
    
    public void setTechnicienComboFilter(ModelContainer<Technicien> techniciens){
        cb_filter_technicien.addItem(null);
        for (Technicien technicien : techniciens) {
            cb_filter_technicien.addItem(technicien);
        }
        cb_filter_technicien.setSelectedIndex(0);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table_incidents = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cb_filter_technicien = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        cb_filter_statut = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cb_filter_priorite = new javax.swing.JComboBox();
        btn_filter = new java.awt.Button();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        table_velo = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        table_typeinc = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        table_techniciens = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        item_velo = new javax.swing.JMenuItem();
        item_technicien = new javax.swing.JMenuItem();
        item_typeinc = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        table_incidents.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(table_incidents);

        jLabel1.setText("Technicien :");
        jPanel5.add(jLabel1);

        jPanel5.add(cb_filter_technicien);

        jLabel2.setText("Statut :");
        jPanel5.add(jLabel2);

        jPanel5.add(cb_filter_statut);

        jLabel3.setText("Priorité :");
        jPanel5.add(jLabel3);

        jPanel5.add(cb_filter_priorite);

        btn_filter.setLabel("Filtrer");
        btn_filter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_filterActionPerformed(evt);
            }
        });
        jPanel5.add(btn_filter);
        btn_filter.getAccessibleContext().setAccessibleName("Filtrer");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Incidents", jPanel1);

        jScrollPane2.setViewportView(table_velo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("Vélos", jPanel2);

        table_typeinc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(table_typeinc);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("Types d'incidents", jPanel3);

        table_techniciens.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(table_techniciens);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 738, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("Techniciens", jPanel4);

        jMenu1.setText("Nouveau");

        item_velo.setText("Vélo");
        item_velo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_veloActionPerformed(evt);
            }
        });
        jMenu1.add(item_velo);

        item_technicien.setText("Technicien");
        item_technicien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_technicienActionPerformed(evt);
            }
        });
        jMenu1.add(item_technicien);

        item_typeinc.setText("Type d'Incident");
        item_typeinc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                item_typeincActionPerformed(evt);
            }
        });
        jMenu1.add(item_typeinc);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane2)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void item_typeincActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_typeincActionPerformed
        TypeIncident typeInc = new TypeIncident();
        typeInc.ajouterObserveur(controleur);
        new TypeIncidentForm(this, true, TypeFormulaire.CREATION, typeInc).setVisible(true);
    }//GEN-LAST:event_item_typeincActionPerformed

    private void item_technicienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_technicienActionPerformed
        Technicien technicien = new Technicien();
        technicien.ajouterObserveur(controleur);
        new TechnicienForm(this, true, TypeFormulaire.CREATION, technicien).setVisible(true);
    }//GEN-LAST:event_item_technicienActionPerformed

    private void item_veloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_item_veloActionPerformed
        Velo velo = new Velo();
        velo.ajouterObserveur(controleur);
        new VeloForm(this, true, TypeFormulaire.CREATION, velo).setVisible(true);
    }//GEN-LAST:event_item_veloActionPerformed

    private void btn_filterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_filterActionPerformed
        controleur.recuperIncidents((Technicien)cb_filter_technicien.getSelectedItem(),
                                    (String)cb_filter_statut.getSelectedItem(), 
                                    (String)cb_filter_priorite.getSelectedItem());
    }//GEN-LAST:event_btn_filterActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button btn_filter;
    private javax.swing.JComboBox cb_filter_priorite;
    private javax.swing.JComboBox cb_filter_statut;
    private javax.swing.JComboBox cb_filter_technicien;
    private javax.swing.JMenuItem item_technicien;
    private javax.swing.JMenuItem item_typeinc;
    private javax.swing.JMenuItem item_velo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTable table_incidents;
    private javax.swing.JTable table_techniciens;
    private javax.swing.JTable table_typeinc;
    private javax.swing.JTable table_velo;
    // End of variables declaration//GEN-END:variables
}
